package com.zikao.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.zikao.article.entity.TagEntity;
import com.zikao.article.entity.TextEntity;
import com.zikao.article.service.PraiseService;
import com.zikao.article.service.TagService;
import com.zikao.article.service.TextService;
import com.zikao.user.entity.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zikao.common.utils.PageUtils;
import com.zikao.common.utils.Query;
import com.zikao.article.dao.ArticleDao;
import com.zikao.article.entity.ArticleEntity;
import com.zikao.article.service.ArticleService;


@Service("articleService")
@Slf4j
public class ArticleServiceImpl extends ServiceImpl<ArticleDao, ArticleEntity> implements ArticleService {

    @Autowired
    private TextService textService;
    @Autowired
    private TagService tagService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ArticleEntity> page = this.page(
                new Query<ArticleEntity>().getPage(params),
                new QueryWrapper<ArticleEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Wrapper queryWrapper) {
        IPage<ArticleEntity> page = this.page(
                new Query<ArticleEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<ArticleEntity> selectByMap(Map<String, Object> params) {
        return baseMapper.selectByMap(params);
    }

    @Override
    public List<ArticleEntity> selectList(Wrapper queryWrapper){
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public void saveArticle(ArticleEntity article) {
        // 保存文本内容
        TextEntity text = new TextEntity();
        text.setContent(article.getContent());
        textService.save(text);
        // 保存文章
        article.setTextId(text.getId());
        baseMapper.insert(article);
    }

    @Override
    public ArticleEntity getArticleById(Long id) {
        // 获取文章内容
        ArticleEntity article = baseMapper.selectById(id);
        TextEntity text = textService.getById(article.getTextId());
        article.setContent(text.getContent());
        // 获取文章标签
        TagEntity tag = tagService.getById(article.getTagId());
        article.setTagName(tag.getName());
        return article;
    }

    @Override
    public List<ArticleEntity> selectBatchIds(List<Long> articleIds) {
        return baseMapper.selectBatchIds(articleIds);
    }

    @Override
    public void updateArticleById(ArticleEntity article) {
        ArticleEntity dbArticle = baseMapper.selectById(article.getId());

        // 修改文本内容
        TextEntity text = new TextEntity();
        text.setId(dbArticle.getTextId());
        text.setContent(article.getContent());
        textService.updateById(text);

        baseMapper.updateById(article);
    }
}